/*
 * @Author: LiYuan
 * @Date: 2021-03-29 11:31:06
 * @LastEditTime: 2021-03-29 11:34:08
 * @LastEditors: LiYuan
 * @Description: 
 * You build it, You run it.
 */
import _ from 'lodash';
import {
  storageGet,
  storageSet
} from '@src/util/storage';

const urlMap = {
  taskList: '/task',
  taskAllot: '/task/allot',
  settlement: '/balance/alreadySettlement',
  taskReview: '/task/review',
  taskClose: '/task/close',
  taskPlanList: '/task/planTask/list',
  taskPool: '/task/taskPool',
  event: '/event',
  eventOrder: '/event/order',
}

const storagePageMap = {
  taskList: 'shb_task_list_page_data',
  taskAllot: 'shb_task_allot_page_data',
  settlement: 'shb_settlement_page_data',
  taskReview: 'shb_task_review_page_data',
  taskClose: 'shb_task_close_page_data',
  taskPlanList: 'shb_task_planlist_page_data',
  taskPool: 'shb_task_taskpool_page_data',
  event: 'shb_event_list_page_data',
  eventOrder: 'shb_event_order_page_data',
};

const EditMixins = {
  methods: {
    conversionUrlWithStorageName(fullUrl) {
      if (!fullUrl) return '';

      let symbol = '?';
      let symbolIndex = fullUrl.indexOf(symbol);
      let url = symbolIndex < 0 ? fullUrl : fullUrl.substr(0, symbolIndex);
      let storageKey = '';

      for (let key in urlMap) {
        if (urlMap[key] == url) {
          storageKey = key;
          break;
        }
      }

      return storagePageMap[storageKey] || '';
    },
    getStoragePageData(fullUrl) {
      let storageKey = this.conversionUrlWithStorageName(fullUrl);

      if (!storageKey) return {};

      let pageData = {};

      try {
        pageData = storageGet(storageKey, '{}');
        pageData = JSON.parse(pageData);
      } catch (err) {
        console.log('hbc: getStoragePageData -> err', err)
      }

      // 存入缓存
      if (Object.keys(pageData).length <= 0) {
        let setPageData = {
          pageSize: 10
        };
        storageSet(storageKey, JSON.stringify(setPageData));
      }

      return pageData;

    },
    joinParams(fullUrl) {
      if (!fullUrl) return '';

      let symbol = '?';
      let symbolIndex = fullUrl.indexOf(symbol);
      let storageData = this.getStoragePageData(fullUrl);
      let url = fullUrl;
      let startIndex = 0;

      // 拼接参数
      if (symbolIndex < 0) {
        for (let key in storageData) {
          startIndex++;
          startIndex > 1
            ? url += `&${key}=${storageData[key]}`
            : url += `?${key}=${storageData[key]}`
        }
      } else {
        for (let key in storageData) {
          url += `&${key}=${storageData[key]}`;
        }
      }

      return url;

    },

  },
};

export default EditMixins;